package cn.yqn.controller;

import cn.yqn.domain.User;
import cn.yqn.result.JsonResult;
import cn.yqn.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JsonResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JsonResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JsonResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JsonResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.success(userService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JsonResult list(){
        return JsonResult.success(userService.selectList(null));
    }


    //查询电影票
    @GetMapping("/movies")
    public JsonResult movies(){
        return JsonResult.success(userService.movies());
    }

    /*
     * @Description: 购买电影票
     **/
    @GetMapping("/buy/{movieId}")
    public JsonResult buy(@PathVariable("movieId") Long movieId){
        return JsonResult.success(userService.buy(movieId));
    }

    /*
     * @Description: 购买电影票
     **/
    @GetMapping("/watch/{movieId}")
    public JsonResult watch(@PathVariable("movieId") Long movieId){
        return JsonResult.success(userService.watch(movieId));
    }


}
